package com.baylor.server.business.interceptor;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
 

import com.baylor.server.business.entity.Admin;

public class CommonInterceptor implements HandlerInterceptor {

	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		// TODO Auto-generated method stub
		boolean isok = false;
		HttpSession session = request.getSession();
		Admin admin =(Admin) session.getAttribute("admin");
		if (admin == null) { 
			isok = false;
			// 跳转到非法访问页面
			response.sendRedirect(request.getContextPath() + "/");
		} else {
			isok = true;
		} 
		return isok;
	}

	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
	}

	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
	}

	public boolean isCheck(String selvertPath) {
		String actionName = selvertPath.substring(
				selvertPath.lastIndexOf("/") + 1, selvertPath.length());
		List<String> anctions = new ArrayList<String>();
		anctions.add("regist");
		anctions.add("regist4");

		anctions.add("regist1");
		anctions.add("validate");
		anctions.add("regist3");
		anctions.add("showSigmaUserInfo");
		anctions.add("toUpdateSigmaUser");
		anctions.add("updateSigmaUser");
		return anctions.contains(actionName);
	}
}
